/**
 * Vuex
 * http://vuex.vuejs.org/zh-cn/intro.html
 */
import Vue from 'vue';
import Vuex from 'vuex';
import Public from 'assets/public.js'
Vue.use(Vuex);
const store = new Vuex.Store({
	state: {
		currentView: "请选择医院",
		orglists: [],
		deptlists: [],
		doclists: [],
		arr1: [],
		arr2: [],
		arr3: [],
		datearr: [],
		dayarr: [], //星期的数组
		doctorName: "",
		doctorphoneNo: "",
		curOrgId: "",
	},
	mutations: {
		// 改变视图
		CHANGE_VIEW(state, biaoshi) {
			state.currentView = biaoshi
		},
		GET_ORGLIST(state, data) {
			state.orglists = data;
		},
		GET_DEPTLIST(state, data) {

			state.deptlists = data;
		},
		GET_DOCLIST(state, data) {
			state.doclists = data;
		},
		GET_DOCPLANLIST(state, arrdata) {
			state.arr1 = arrdata.arr1;
			state.arr2 = arrdata.arr2;
			state.arr3 = arrdata.arr3;
			state.datearr = arrdata.datearr;
			state.doctorName = arrdata.doctorName;
			state.doctorphoneNo = arrdata.doctorphoneNo;
			state.dayarr = arrdata.dayarr;
		},
		CURORGID(state, curOrgId) {
			state.curOrgId = curOrgId;

		},
		// 发送消息
		// SEND_MESSAGE({
		// 	sessions,
		// 	currentSessionId
		// }, content) {
		// 	let session = sessions.find(item => item.id === currentSessionId);
		// 	session.messages.push({
		// 		content: content,
		// 		date: new Date(),
		// 		self: true
		// 	});
		// },

	}


});

// store.watch(
// 	(state) => state.sessions,
// 	(val) => {
// 		console.log('CHANGE: ', val);
// 		localStorage.setItem('vue-chat-session', JSON.stringify(val));
// 	}, {
// 		deep: true
// 	}
// );

export default store;
export const actions = {
	//改变组件之间的切换
	changeView: ({
		dispatch
	}, biaoshi) => dispatch('CHANGE_VIEW', biaoshi),
	//获取机构列表
	getorgLists: function({
		dispatch
	}) {
		$.when(Public.commonajax("coms.doctorPlanService", "queryDoctorPlanOrgList", "[[]]")).done(function(res) {
			dispatch('GET_ORGLIST', res.body)
		});
	},
	//获取科室列表
	getdeptList: function({
		dispatch
	}, item) {
		dispatch('CURORGID', item.orgId)
		$.when(Public.commonajax("coms.doctorPlanService", "queryDoctorPlanDeptList", "['" + item.orgId + "']")).done(function(res) {
			dispatch('GET_DEPTLIST', res.body)
		});
	},
	//获取医生列表
	getDoclist: function({
		dispatch
	}, item) {

		$.when(Public.commonajax("coms.doctorPlanService", "queryDoctorPlanDoctorList", "['" + item.deptId + "']")).done(function(res) {
			dispatch('GET_DOCLIST', res.body)
		});

	},
	////获取医生排班计划
	getdcoPlanlist: function({
		dispatch
	}, item) {

		$.when(Public.commonajax("coms.doctorPlanService", "queryDoctorPlanByDoctorId", "['" + this.$store.state.curOrgId + "','" + item.doctorId + "']")).done(function(res) {

			var curmoday = getcurWeekmonday();
			var datearr = mondaytosundayarr(curmoday);
			var dayarr = getsevenday(curmoday);
			var amarr = [],
				pmarr = [],
				ntarr = [];
			amarr = res.body.filter(function(item) {
				return item.planClass == 1;
			});
			pmarr = res.body.filter(function(item) {
				return item.planClass == 2;
			});
			ntarr = res.body.filter(function(item) {
				return item.planClass == 3;
			});
			var arr1 = getdataarr(amarr);
			var arr2 = getdataarr(pmarr);
			var arr3 = getdataarr(ntarr);
			var doctorName = item.doctorName;
			var doctorphoneNo = item.phoneNo;
			var data = {
				arr1,
				arr2,
				arr3,
				datearr,
				doctorName,
				doctorphoneNo,
				dayarr,
			}

			dispatch('GET_DOCPLANLIST', data) //mutations的GET_DOCPLANLIST接收
		});

	},
	// sendMessage: ({
	// 	dispatch
	// }, content) => dispatch('SEND_MESSAGE', content),
	// selectSession: ({
	// 	dispatch
	// }, id) => dispatch('SELECT_SESSION', id),
	// search: ({
	// 	dispatch
	// }, value) => dispatch('SET_FILTER_KEY', value),
	//聊天chat的actions结束
};

function getdataarr(arr) { //通过另外一个数组的索引，来填充数据，从而活动上午下午晚上对应的数据
	var indexarr = [];
	var temarr = [{
		text: 'noconent'
	}, {
		text: 'noconent'
	}, {
		text: 'noconent'
	}, {
		text: 'noconent'
	}, {
		text: 'noconent'
	}, {
		text: 'noconent'
	}, {
		text: 'noconent'
	}];
	var curmoday = getcurWeekmonday();
	var datearr = mondaytosundayarr(curmoday);
	if (arr && arr.length > 0) {
		$.each(datearr, function(index, el) {
			$.each(arr, function(index2, el2) {
				if (el == el2.planDate.substring(0, 10)) {
					indexarr.push(index);

				}
			});
		});

		$.each(indexarr, function(index, el) {
			temarr[el] = arr[index]
		});

	}
	return temarr
}
// 制作今天开始之后7天的日期的函数
function mondaytosundayarr(monday) {
	var now = new Date(monday);
	var arr = [];
	var date = now.getDate();
	for (let i = 0; i < 7; i++) {
		// now.setDate(date + i)
		var arritem = addDate(monday, i);
		arr.push(arritem)
			// arr.push(Public.formatDate(now))
	}
	return arr;
}
// 制作今天开始之后7天的星期几的函数
function getsevenday(monday) {

	var arr = [];
	for (let i = 0; i < 7; i++) {

		var arritem = addDate(monday, i);
		var date = new Date(arritem);
		var day = date.getDay();
		switch (day) {
			case 0:
				day = "星期日";
				break;
			case 1:
				day = "星期一";
				break;
			case 2:
				day = "星期二";
				break;
			case 3:
				day = "星期三";
				break;
			case 4:
				day = "星期四";
				break;
			case 5:
				day = "星期五";
				break;
			case 6:
				day = "星期六";
				break;
		}
		arr.push(day)

	}
	return arr;
}

function getcurWeekmonday() {
	var now = new Date();
	// var day = now.getDay();
	// now.setDate(now.getDate() - (day - 1)); //本周的周一
	return Public.formatDate(now);
}
// 日期增减函数
function addDate(date, days) { //days往前算就传入负数，往后算就传入正数
	var d = new Date(date);
	d.setDate(d.getDate() + days);
	var month = d.getMonth() + 1;
	var day = d.getDate();
	if (month < 10) {
		month = "0" + month;
	}
	if (day < 10) {
		day = "0" + day;
	}
	var val = d.getFullYear() + "-" + month + "-" + day;
	return val;
}